use strict;
use warnings;
use Time::HiRes qw(sleep);
use Test::WWW::Selenium;
use Test::More "no_plan";
use Test::Exception;

my $sel = Test::WWW::Selenium->new( host => "localhost", 
                                    port => 4444, 
                                    browser => "*firefox", 
                                    browser_url => "http://localhost.com" );

$sel->open_ok("/");
#$sel->click_ok("log_in_top");
$sel->wait_for_page_to_load_ok("30000");
$sel->is_element_present_ok("link=Log out");
$sel->click_ok("link=Administration");
$sel->wait_for_page_to_load_ok("30000");
$sel->click_ok("link=Bug Prediction");
$sel->text_is("link=Change Severity Map", "Change Severity Map");
$sel->click_ok("link=Change Severity Map");
WAIT: {
    for (1..60) {
        if (eval { $sel->is_element_present("severityMap") }) { pass; last WAIT }
        sleep(1);
    }
    fail("timeout");
}
$sel->text_is("//td[\@id='content']/h3", "Change Severity Map");
$sel->text_is("//td[\@id='content']/p", "You can specify which severities are handled and on which classes they are mapped. Place each map on it's own line with the syntax \"severity:class\", the severity map is handled from top to bottom in case you specify duplicate severities or classes.");
$sel->type_ok("severityMap", "enhancement:ns\ntrivial:ns\nminor:ns\nnormal:ns\nblocker:s\ncritical:s\nmajor:s");
$sel->click_ok("severityMap_update");
WAIT: {
    for (1..60) {
        if (eval { !$sel->is_text_present("You can specify which severities are handled and on which classes they are mapped. Place each map on it's own line with the syntax \"severity:class\", the severity map is handled from top to bottom in case you specify duplicate severities or classes.") }) { pass; last WAIT }
        sleep(1);
    }
    fail("timeout");
}
$sel->is_text_present_ok("The severity map has been changed, please update the bug prediction models!");
$sel->click_ok("link=Change Severity Map");
WAIT: {
    for (1..60) {
        if (eval { $sel->is_element_present("severityMap") }) { pass; last WAIT }
        sleep(1);
    }
    fail("timeout");
}
$sel->value_is("severityMap", "enhancement:ns\ntrivial:ns\nminor:ns\nnormal:ns\nblocker:s\ncritical:s\nmajor:s");
